<!-- 字典管理页 -->
<template>
  <!-- 字典数据搜索卡片 -->
  <el-card style="margin:20px;">
    <template #header>
      <el-form :inline="true">
      <el-form-item label="字典名称" style="margin-top: 20px">
        <el-input placeholder="请输入字典名称" style="width:220px;"
                  v-model="searchForm.name" @keydown.enter="searchDict"></el-input>
        <el-input v-show="false"/>
      </el-form-item>
      <el-button style="float: right; margin-left: 15px; margin-top: 20px; margin-right: 5px"
                 @click="resetSearch">
        <el-icon style="margin-right: 5px"><Refresh /></el-icon>重置
      </el-button>
      <el-button type="primary" style="float: right; margin-top: 20px"
                 @click="searchDict">
        <el-icon style="margin-right: 5px"><Search /></el-icon>查询
      </el-button>
      </el-form>
    </template>
    <div style="margin:10px 0px 40px 10px;">
      字典列表
      <el-button type="primary" style="float: right;margin-top:-3px;" @click="addDict">
        <el-icon style="margin-right: 5px"><Plus /></el-icon>
        新增字典
      </el-button>
    </div>
    <el-table :data="dictArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="name" label="字典名称" align="center"/>
      <el-table-column prop="identification" label="字典标识" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="showDictOption(scope.row.id)">
            {{scope.row.identification}}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注信息" align="center"/>
      <el-table-column prop="createTime" label="创建时间" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editDict(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteDict(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!-- 保存字典弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible"
             style="width:1000px; padding:40px;"
             :before-close="handleClose">
    <div style="height: 30px">
      <el-button type="warning" style="float: right; margin-left: 15px" @click="handleClose">
        <el-icon style="margin-right: 5px"><Back /></el-icon>返回
      </el-button>
      <el-button type="primary" style="float: right" @click="saveDict">
        <el-icon style="margin-right: 5px"><Collection /></el-icon>保存
      </el-button>
    </div>
    <hr>
    <el-form label-width="80px" label-position="top">
      <el-form-item label="字典名称">
        <el-input placeholder="请输入字典名称" v-model="saveDictForm.name"></el-input>
      </el-form-item>
      <el-form-item label="字典标识">
        <el-input placeholder="请输入字典标识" v-model="saveDictForm.identification"></el-input>
      </el-form-item>
      <el-form-item label="备注信息">
        <el-input placeholder="请输入备注信息" v-model="saveDictForm.remark"></el-input>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";
import router from "@/router";

//定义对象保存搜索条件
const searchForm = ref({name:''});
//定义数组保存字典数据
const dictArr = ref([]);
//定义搜索字典的方法
const searchDict = () => {
  console.log(searchForm.value)
  let data = qs.stringify(searchForm.value);
  axios.get(BASE_URL+'/v1/dict/select?'+data).then((response)=>{
    if (response.data.code === 2000) {
      dictArr.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg)
    }
  })
}
onMounted(() => {
  searchDict();
})

//定义重置搜索的方法
const resetSearch = () => {
  searchForm.value = {name:''};
  searchDict();
}

//定义变量控制弹窗是否出现
const dialogVisible = ref(false);
//定义变量控制弹窗标题
const dialogTitle = ref('新增字典');
//定义对象保存字典表单数据
const saveDictForm = ref({name:'',identification:'',remark:''});
//定义处理弹窗关闭的方法
const handleClose = () => {
  if(confirm('您是否要关闭本弹窗?')){
    saveDictForm.value = {name:'',identification:'',remark:''};
    dialogVisible.value = false;
  }
}
//点击“新增字典”触发
const addDict = () => {
  dialogVisible.value = true;
  dialogTitle.value = '新增字典';
}
//定义保存字典的方法
const saveDict = () => {
  let data = qs.stringify(saveDictForm.value);
  axios.post(BASE_URL+'/v1/dict/save/',data).then((response)=>{
    if (response.data.code === 2000) {
      ElMessage.success('保存成功！');
      saveDictForm.value = {name:'',identification:'',remark:''};
      dialogVisible.value = false;
      searchDict();
    } else {
      ElMessage.error(response.data.msg)
    }
  })
}

//定义编辑字典的方法
const editDict = (id) => {
  dialogTitle.value = '编辑字典';
  dialogVisible.value = true;
  axios.get(BASE_URL+'/v1/dict/select?id='+id).then((response)=>{
    if (response.data.code === 2000) {
      saveDictForm.value = response.data.data[0];
    } else {
      ElMessage.error(response.data.msg)
    }
  })
}

//定义删除字典的方法
const deleteDict = (id) => {
  if(confirm('您确定要删除该字典吗？')){
    axios.post(BASE_URL+'/v1/dict/delete/'+id).then((response)=>{
      if (response.data.code === 2000) {
        ElMessage.success('删除成功！');
        searchDict();
      } else {
        ElMessage.error(response.data.msg)
      }
    })
  }
}

//定义加载指定字典下所有字典项的方法
const showDictOption = (id)=>{
  router.push('/dictOption?id='+id);
}
</script>

<style>

</style>